package com.andy.toolbox.map.testrecycler;

import android.support.annotation.NonNull;
import android.view.View;

/**
 * Implementation of {@link CarouselLayoutManager.PostLayoutListener} that makes interesting scaling of items. <br />
 * We are trying to make items scaling quicker for closer items for center and slower for when they are far away.<br />
 * Tis implementation uses atan function for this purpose.
 */
public class CustomZoomPostLayoutListener implements CarouselLayoutManager.PostLayoutListener {

    private static final float FLAG = 2.2f;

    private static final float TRANSLATE_FLAG = 0.4f;

    @Override
    public ItemTransformation transformChild(@NonNull final View child, final float itemPositionToCenterDiff, final int orientation) {
        final float scale = 1 / (Math.abs(itemPositionToCenterDiff) * FLAG + 1);
        final float translateY;
        final float translateX;
        if (CarouselLayoutManager.VERTICAL == orientation) {
            final float translateYGeneral = child.getMeasuredHeight() * (1 - scale) * TRANSLATE_FLAG;
            translateY = itemPositionToCenterDiff * translateYGeneral;
            translateX = 0;
        } else {
            final float translateXGeneral = child.getMeasuredWidth() * (1 - scale) * TRANSLATE_FLAG;
            translateX = itemPositionToCenterDiff * translateXGeneral;
            translateY = 0;
        }
        return new ItemTransformation(scale, scale, translateX, translateY);
    }
}